import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:flutter/foundation.dart';

part 'counter_event.dart';
part 'counter_state.dart';

// business logic code
// single state
class CounterBloc extends Bloc<CounterEvent, CounterState> {
  CounterState get _currentCounterState => CounterState(_title, _count);

  String _title = "default";
  int _count = 0;

  CounterBloc() : super(CounterState("default", 0));

  // process actions.
  @override
  Stream<CounterState> mapEventToState(CounterEvent event) async* {
    if (event is ChangeTitleEvent) {
      // yield CounterState(title: event.title, count: state.count);
      _title = event.title;
      yield _currentCounterState;
    } else if (event is AddCountEvent) {
      // yield CounterState(title: state.title, count: ++state.count);
      _count = state.count + 1;
      yield _currentCounterState;
    }
  }
}
